﻿using AmiDraw.Editor.Model;
using AmiDraw.Editor.View.Dialog;
using AmiDraw.Lib;
using Panuon.UI.Silver;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AmiDraw.Editor.Component
{
    /// <summary>
    /// CardItem.xaml 的交互逻辑
    /// </summary>
    public partial class CardItem : UserControl
    {
        public ObservableCard CardData
        {
            get { return (ObservableCard)GetValue(CardDataProperty); }
            set { SetValue(CardDataProperty, value); }
        }

        public static readonly DependencyProperty CardDataProperty =
            DependencyProperty.Register("CardData", typeof(ObservableCard), typeof(CardItem), new PropertyMetadata(new ObservableCard()));

        //定义委托
        public delegate void DeleteSelfHandle(object sender, EventArgs e);
        //定义事件
        public event DeleteSelfHandle DeleteSelfEvent;


        public CardItem()
        {
            InitializeComponent();
            MainGrid.DataContext = this;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var olddata = (ObservableCard)CardData.Clone();
            CardInfoDialog dialog = new((ObservableCard)CardData.Clone());
            //dialog.ViewModel = (ObservableCard)CardData;


            if (dialog.ShowDialog() == true)
            {
                if (dialog.ViewModel.ObservableCard == null)
                {
                    DeleteSelfEvent?.Invoke(olddata, new EventArgs());
                    return;
                }
                this.CardData.ReplaceBy(dialog.ViewModel.ObservableCard);
            }
            else
            {
                this.CardData = olddata;
            }

            
        }
    }

}
